/* 
 * 
 * Created on 27 Oct 2011 by simonk
 * 
 * Copyright: Multicom Products Ltd. 2011 Bristol, England
 */
package utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 *
 * @author simonk
 */
public class Shell {

	public static String runCmd(String cmd) throws Exception
	{
		String[] cmdarr = {
				"/bin/sh",
				"-c",
				cmd };
		Runtime run = Runtime.getRuntime();
		Process p = run.exec(cmdarr);
		BufferedReader input = new BufferedReader(
				new InputStreamReader(p.getInputStream()));
		String s = input.readLine();
		input.close();
		p.waitFor();
		p.destroy();
		return s;
	}

	public static String[] runOneCmd() throws IOException,
			InterruptedException {
		String[] data = new String[4];
		Process child = Runtime.getRuntime().exec("showstats -v");
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				child.getInputStream()));
		// String s = "";
		String line;
		while ((line = reader.readLine()) != null) {
			// System.out.println("Stdout: " + line);
			if (line.contains("Active Jobs:")) {
				// jobs
				data[0] = line.substring("Active Jobs:".length() + 1).trim();
			}else if (line.contains("Eligible/Idle Jobs")){
					String g = line
					.substring("Eligible/Idle Jobs:".length() + 1)
					.trim();
				data[1] = g.split("/")[0]; 
			} else if (line.contains("Current Active/Total Procs:")) {
				data[2] = line.substring(
						"Current Active/Total Procs:".length() + 1,
						line.indexOf('(')).trim();
			} else if (line.contains("Current Active/Total Nodes:")) {
				data[3] = line.substring(
						"Current Active/Total Nodes:".length() + 1,
						line.indexOf('(')).trim();
			}
		}
		child.waitFor();
		return data;
	}

}
